package com.dhms.vostok1.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.dhms.workbench.R
import com.dhms.vostok1.data.work_bench.WorkBenchData
import com.dhms.vostok1.data.work_bench.WorkBenchItem
import com.dhms.vostok1.data.work_bench.WorkBenchItemType

class WorkBenchViewModel : ViewModel() {
    private val _workBenchListData = MutableLiveData<List<WorkBenchData>>(listOf())
    val workBenchListData: LiveData<List<WorkBenchData>> =
        _workBenchListData

    private val maxCount = 4

    fun getWorkBenchList(privileges: String?) {

        demoTest()

        return

//        var workBenchList = mutableListOf<WorkBenchData>()
//
//        var deviceList = mutableListOf<WorkBenchItem>()
//        var approvalList = mutableListOf<WorkBenchItem>()
//        var applyList = mutableListOf<WorkBenchItem>()
//
//
//        /*这里判断菜单权限*/
//        /*设备管理*/
//        if (checkPermission(privileges, AppFeatureManager.damagePermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_damage_item_title,
//                    R.drawable.ic_peril,
//                    WorkBenchItemType.DAMAGE
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.faultPermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_fault_item_title,
//                    R.drawable.ic_fault,
//                    WorkBenchItemType.FAULT
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.spotPatrolPermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_spot_patrol_item_title,
//                    R.drawable.ic_spot_patrol,
//                    WorkBenchItemType.SPOT_PATROL
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.lubricationPermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_lubrication_item_title,
//                    R.drawable.ic_lubrication,
//                    WorkBenchItemType.LUBRICATION
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.maintenancePermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_maintenance_item_title,
//                    R.drawable.ic_maintenance,
//                    WorkBenchItemType.MAINTENANCE
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.repairPermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_repair_item_title,
//                    R.drawable.ic_repair,
//                    WorkBenchItemType.REPAIR
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.knowledgePermission)) {
//            deviceList.add(
//                WorkBenchItem(
//                    R.string.work_bench_knowledge_item_title,
//                    R.drawable.ic_knowledge,
//                    WorkBenchItemType.KNOWLEDGE
//                )
//            )
//        }
//
//        /*审批*/
//        if (checkPermission(privileges, AppFeatureManager.approvalPermission)) {
//            approvalList.add(
//                WorkBenchItem(
//                    R.string.work_bench_approval_item_title,
//                    R.drawable.ic_approval,
//                    WorkBenchItemType.APPROVAL
//                )
//            )
//        }
//        /*申请*/
//        if (checkPermission(privileges, AppFeatureManager.purchasePermission)) {
//            applyList.add(
//                WorkBenchItem(
//                    R.string.work_bench_purchase_item_title,
//                    R.drawable.ic_purchase,
//                    WorkBenchItemType.PURCHASE
//                )
//            )
//        }
//        if (checkPermission(privileges, AppFeatureManager.warehousingPermission)) {
//            applyList.add(
//                WorkBenchItem(
//                    R.string.work_bench_warehousing_item_title,
//                    R.drawable.ic_warehousing,
//                    WorkBenchItemType.WAREHOUSING
//                )
//            )
//        }
//
//
//        fillWorkBenchItem(deviceList)
//        fillWorkBenchItem(approvalList)
//        fillWorkBenchItem(applyList)
//
//        createWorkBenchData(deviceList, "设备管理")?.let {
//            workBenchList.add(it)
//        }
//
//        createWorkBenchData(approvalList, "审批")?.let {
//            workBenchList.add(it)
//        }
//
//        createWorkBenchData(applyList, "申请")?.let {
//            workBenchList.add(it)
//        }
//
//        _workBenchListData.value = workBenchList
    }

    private fun checkPermission(privileges: String?, permission: List<String>): Boolean {
        privileges?.let {
            permission.forEach { p ->
                if (privileges.contains(p)) {
                    return true
                }
            }
        }
        return false
    }

    private fun fillWorkBenchItem(items: MutableList<WorkBenchItem>) {
        if (items.isNotEmpty()) {
            val to = maxCount - items.count()
            for (i in 0 until to) {
                items.add(WorkBenchItem())
            }
        }
    }

    private fun createWorkBenchData(
        items: MutableList<WorkBenchItem>,
        name: String
    ): WorkBenchData? {
        if (items.isEmpty()) {
            return null
        }
        return WorkBenchData(
            name,
            items
        )
    }

    private fun demoTest() {
        var workBenchList = mutableListOf<WorkBenchData>(
            WorkBenchData(
                "设备管理",
                listOf<WorkBenchItem>(
                    WorkBenchItem(
                        R.string.work_bench_damage_item_title,
                        R.drawable.ic_peril,
                        WorkBenchItemType.DAMAGE
                    ),
                    WorkBenchItem(
                        R.string.work_bench_fault_item_title,
                        R.drawable.ic_fault,
                        WorkBenchItemType.FAULT
                    ),
                    WorkBenchItem(
                        R.string.work_bench_spot_patrol_item_title,
                        R.drawable.ic_spot_patrol,
                        WorkBenchItemType.SPOT_PATROL
                    ),
                    WorkBenchItem(
                        R.string.work_bench_lubrication_item_title,
                        R.drawable.ic_lubrication,
                        WorkBenchItemType.LUBRICATION
                    ),
                    WorkBenchItem(
                        R.string.work_bench_maintenance_item_title,
                        R.drawable.ic_maintenance,
                        WorkBenchItemType.MAINTENANCE
                    ),
                    WorkBenchItem(
                        R.string.work_bench_repair_item_title,
                        R.drawable.ic_repair,
                        WorkBenchItemType.REPAIR
                    ),
                    WorkBenchItem(
                        R.string.work_bench_knowledge_item_title,
                        R.drawable.ic_knowledge,
                        WorkBenchItemType.KNOWLEDGE
                    )
                )
            ),
//            WorkBenchData(
//                "审批",
//                listOf<WorkBenchItem>(
//                    WorkBenchItem(
//                        R.string.work_bench_approval_item_title,
//                        R.drawable.ic_approval,
//                        WorkBenchItemType.APPROVAL
//                    ),
//                    WorkBenchItem(), WorkBenchItem(), WorkBenchItem()
//                )
//            ),
            WorkBenchData(
                "资产管理",
                listOf<WorkBenchItem>(
                    WorkBenchItem(
                        R.string.work_bench_warehouse_in_item_title,
                        R.drawable.ic_warehouse_in,
                        WorkBenchItemType.WAREHOUSE_IN
                    ),
                    WorkBenchItem(
                        R.string.work_bench_equipment_return_item_title,
                        R.drawable.ic_equipment_return,
                        WorkBenchItemType.EQUIPMENT_RETURN
                    ),
                    WorkBenchItem(
                        R.string.work_bench_warehouse_out_item_title,
                        R.drawable.ic_warehouse_out,
                        WorkBenchItemType.WAREHOUSE_OUT
                    ),
                    WorkBenchItem(
                        R.string.work_bench_equipment_requisition_item_title,
                        R.drawable.ic_equipment_requisition,
                        WorkBenchItemType.EQUIPMENT_REQUISITION
                    ),
                )
            ),
            WorkBenchData(
                "申请",
                listOf<WorkBenchItem>(
                    WorkBenchItem(
                        R.string.work_bench_purchase_item_title,
                        R.drawable.ic_purchase,
                        WorkBenchItemType.PURCHASE
                    ),
                    WorkBenchItem(
                        R.string.work_bench_warehousing_item_title,
                        R.drawable.ic_return,
                        WorkBenchItemType.WAREHOUSING
                    ),
                    WorkBenchItem(), WorkBenchItem()
                )
            )
        )

        _workBenchListData.value = workBenchList
    }

}